package com.yndsoft.jc.ui.memberpage.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.KeyValue;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/5.
 */

public class HelpCenterActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.help_center_parent)
    LinearLayout help_center_parent;
    List<KeyValue> mDatas = new ArrayList<>();
    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_center);
        inflater = LayoutInflater.from(mContext);

        setViews();
        getData();
        setListener();
    }

    private void getData() {
        mHttpUtil.getHelpCategory(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {

                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1:
                        Type type = new TypeToken<List<KeyValue>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        mDatas = (ArrayList<KeyValue>) fromJson;

                        for (int i = 0; i < mDatas.size(); i++) {
                            help_center_parent.addView(createView(mDatas.get(i).getValue(), mDatas.get(i).getKey()));
                        }
                        break;
                }
            }
        }, "helptype");
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.help_center));
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
        }
    }


    public static final String HELP_CATEGORY_ID = "PID";
    public static final String HELP_TITLE = "title";

    private View createView(final String txt, final int id) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(mContext, 50));
        RelativeLayout view = new RelativeLayout(this);
        view.setBackgroundColor(getResources().getColor(R.color.white));
        view.setLayoutParams(lp);//设置布局参数

        RelativeLayout item = (RelativeLayout) inflater.inflate(R.layout.item_help_category, null);
        TextView help_item_tv = item.findViewById(R.id.help_item_tv);
        AppUtils.setText(help_item_tv, txt);
        view.addView(item);
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HelpCenterActivity.this, MonitoringGuideActivity.class);
                intent.putExtra(HELP_TITLE, txt);
                intent.putExtra(HELP_CATEGORY_ID, id);
                startActivity(intent);
            }
        });
        return view;
    }
}
